﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace AllInOne.Pages
{
    /// <summary>
    /// ConfirmLicenceInfoPage.xaml 的交互逻辑
    /// </summary>
    public partial class ConfirmLicenceInfoPage : Page
    {

        private MainWindow parent;
        public ConfirmLicenceInfoPage()
        {
            InitializeComponent();
        }

        public ConfirmLicenceInfoPage(MainWindow parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        public void InitPage() {
            this.imgTitle.Source = new BitmapImage(new Uri(this.parent.businessInfo.getImgTitleUrl()));

            RefreshInfo();
            this.parent.PlaySound(Properties.Resources.male_confirm_license, Properties.Resources.female_confirm_license);

        }

        public void RefreshInfo()
        {
            this.txtName.Text = string.Format("姓名: {0}", this.parent.drivingLicence.xm);
            this.txtIdNumber.Text = string.Format("驾驶证号: {0}", this.parent.drivingLicence.sfzmhm);
            this.txtAddress.Text = string.Format("联系地址: {0}", this.parent.drivingLicence.lxzsxxdz);
            this.txtPhone.Text = string.Format("手机号码: {0}", this.parent.drivingLicence.sjhm);
            if (this.parent.businessInfo.sjhm != null) {
                this.txtPhone.Text = string.Format("手机号码: {0}", this.parent.businessInfo.sjhm);
            }

        }

        private void BtnConfirm_MouseUp(object sender, MouseButtonEventArgs e)
        {
            JObject json = ServiceApi.hqzp(this.parent.drivingLicence.sfzmhm);
            string result = json["result"].ToString();
            if (result == "ok")
            {
                string zp = json["tp"].ToString();
                try
                {
                    System.Drawing.Bitmap zpImage = ArcSoftFace.Utils.ImageUtil.StringToBitmap(zp);
                    zpImage.Save("tempZp.png");
                }
                catch (Exception ex)
                {
                    this.parent.StartTakePhoto();
                    return;
                }
                long zpId = long.Parse(json["zpId"].ToString());
                this.parent.ShowConfirmPhotoPage(zp,zpId);
            }
            else {
                this.parent.StartTakePhoto();
            }
        }

        private void BtnModifyPhone_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ShowModifyPhone();
        }

        public void SetSjhm(string sjhm)
        {
            this.txtPhone.Text = "手机号码: "+sjhm;
        }

        private void BtnBackToIndex_MouseUp(object sender, MouseButtonEventArgs e)
        {
            this.parent.ResetIndex();
        }
    }
}
